#!/bin/bash

# 设置日期格式为YYYYMMDD
DATE=$(date +"%Y%m%d")
NEW_IMAGE="registry.cn-beijing.aliyuncs.com/dolphi/heygem_dolphi:${DATE}"
COMPOSE_FILE="docker-compose.yml"

# 显示脚本说明
echo "====================================================="
echo "更新docker-compose.yml中的镜像版本"
echo "====================================================="
echo "新镜像: ${NEW_IMAGE}"
echo "配置文件: ${COMPOSE_FILE}"
echo "====================================================="

# 备份原始文件
cp ${COMPOSE_FILE} "${COMPOSE_FILE}.bak"
echo "已备份原始配置文件为 ${COMPOSE_FILE}.bak"

# 使用sed更新镜像版本
# 匹配形如 "image: registry.cn-beijing.aliyuncs.com/dolphi/heygem_dolphi:X.X" 的行
# 并替换为新的镜像版本
sed -i "s|image: registry.cn-beijing.aliyuncs.com/dolphi/heygem_dolphi:[0-9\.]*|image: ${NEW_IMAGE}|g" ${COMPOSE_FILE}

# 检查是否成功更新
if grep -q "${NEW_IMAGE}" ${COMPOSE_FILE}; then
    echo "====================================================="
    echo "成功更新docker-compose.yml中的镜像版本!"
    echo "旧配置已备份为 ${COMPOSE_FILE}.bak"
    echo "请使用 'docker-compose up -d' 重新启动服务应用新镜像"
    echo "====================================================="
else
    echo "更新失败，请检查文件格式或手动更新"
    # 恢复备份
    mv "${COMPOSE_FILE}.bak" ${COMPOSE_FILE}
    echo "已恢复原始配置文件"
    exit 1
fi 